package weaver.ihg.schedule;

import cn.hutool.core.date.DateUtil;
import weaver.conn.RecordSet;
import weaver.file.Prop;
import weaver.general.BaseBean;
import weaver.integration.util.JSONUtil;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据代码查询 酒店列表基础数据，分给中心岗位异动表数据，和酒店岗位异动表数据
 */
public class SelectbaseHtoleINfoToTwoTable extends BaseCronJob {
    private String sqlWhere;

    public String getSqlWhere() {
        return sqlWhere;
    }

    public void setSqlWhere(String sqlWhere) {
        this.sqlWhere = sqlWhere;
    }

    //

    @Override
    public void execute() {
        BaseBean baseBean=new BaseBean();
        String sql="select * from uf_hotelinfo "+sqlWhere;//查询此基础表的所有字段数据，*代替
        baseBean.writeLog("SelectbaseHtoleINfoToTwoTable====>"+sql+"===>sqlWhere===>"+sqlWhere);
        RecordSet recordSet=new RecordSet();
        RecordSet recordSet2=new RecordSet();
        RecordSet recordSet3=new RecordSet();
        RecordSet recordSet4=new RecordSet();
        recordSet.execute(sql);
        while (recordSet.next()){
            //遍历酒店岗位选择
            String holidex=recordSet.getString("holidex");//酒店代码
            String holideid=recordSet.getString("id");//酒店id
            String sqluf_hotelpositions="select * from uf_hotelpositions";//中心岗位的数据,酒店岗位的数据
            baseBean.writeLog("sqluf_hotelpositions===>"+sqluf_hotelpositions);
            recordSet2.execute(sqluf_hotelpositions);
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append("insert into uf_zczx (INNCODE,SupportRole,bm,Name,Merlinid,wxid,formmodeid) values (?,?,?,?,?,?,?)");
            StringBuilder stringBuilderTwo=new StringBuilder();
            stringBuilderTwo.append("insert into uf_Master_Hotel_Role (inncode,hotelrole,Name,Merlinid,wxid,email,gxrq,formmodeid) values  (?,?,?,?,?,?,?,?)");
            List<List> params = new ArrayList<>();
            List<List> paramsTwo = new ArrayList<>();
            while (recordSet2.next()) {
                List<Object> beanParams =  new ArrayList<>();
                List<Object> beanParamsTwo =  new ArrayList<>();
                int id=recordSet2.getInt("id");
                String gwzdm = recordSet2.getString("gwzdm");//岗位字段名；
                //找到酒店岗位选择=此字段的，找到所属岗位，
                int gwlb = recordSet2.getInt("gwlb");//岗位类别；
                baseBean.writeLog("====>gwmc===>"+gwzdm+"====>gwlb===>"+gwlb);
                String gwmcValue = recordSet.getString(gwzdm);
                baseBean.writeLog("gwmcValue--->"+gwmcValue);
                String []gwmcValueStr=gwmcValue.split(",");
                baseBean.writeLog("gwmcValueStr--->"+gwmcValueStr);
                Integer gwValueHrmId= Integer.parseInt(gwmcValueStr[0].equals("")?"0":gwmcValueStr[0])==0?null:Integer.parseInt(gwmcValueStr[0].equals("")?"0":gwmcValueStr[0]);
                baseBean.writeLog("gwValueHrmId===>"+gwValueHrmId);
                String hrmSql="select loginid,DEPARTMENTID,lastname,email,id from hrmresource where id in (?)";
                baseBean.writeLog("gwmcValue--->"+gwmcValue);
                baseBean.writeLog("hrmSql--->"+hrmSql);
                recordSet3.executeQuery(hrmSql,gwValueHrmId);
                Integer departmentid = null;
                Integer name=null;String merlin_id="",wxid="",email="",gxrq= DateUtil.now();
                if(recordSet3.next()){
                    baseBean.writeLog("intoTHis===>userId===>");
                    //找hrm_info 根据userid值
                    String userid=recordSet3.getString("loginid");
                    wxid=userid;
                    departmentid=recordSet3.getInt("DEPARTMENTID");
                    name=recordSet3.getInt("id");
                    email=recordSet3.getString("email");
                    baseBean.writeLog("userid===>"+userid+"===departmentid===>"+departmentid+"===>name===>"+name+"==email===>"+email);
                    String findSqlStr="select * from  uf_hrinfo  where userid=?";
                    recordSet4.executeQuery(findSqlStr,userid);
                    if(recordSet4.next()){
                        merlin_id=recordSet4.getString("merlin_id");
                        baseBean.writeLog("into merlin_id==>"+merlin_id);
                    }
                }
//                baseBean.writeLog("gwmcValue===>"+gwmcValue);
//                beanParams.add(gwmcValue);
                if(gwlb==0){//中心岗位
                    beanParams.add(holideid);
                    beanParams.add(id);
                    beanParams.add(departmentid);
                    beanParams.add(name);
                    beanParams.add(merlin_id);
                    beanParams.add(wxid);
                    beanParams.add(Integer.parseInt(Prop.getPropValue("cofigHostApi","uf_zczxId")));
                    params.add(beanParams);
                }else if(gwlb==1){//酒店岗位
                    beanParamsTwo.add(holideid);
                    beanParamsTwo.add(id);
                    beanParamsTwo.add(gwValueHrmId);
                    beanParamsTwo.add(merlin_id);
                    beanParamsTwo.add(wxid);
                    beanParamsTwo.add(email);
                    beanParamsTwo.add(gxrq);
                    beanParamsTwo.add(Integer.parseInt(Prop.getPropValue("cofigHostApi","uf_Master_Hotel_RoleId")));
                    paramsTwo.add(beanParamsTwo);
                }
            }
            baseBean.writeLog("stringBuilderSql2===>"+stringBuilder.toString()+"====>params===>"+ JSONUtil.toJSONString(params));
            baseBean.writeLog("stringBuilderSqlTwoSql2====>"+stringBuilderTwo.toString()+"====>paramsTwo===>"+JSONUtil.toJSONString(paramsTwo));

            if(!params.isEmpty()){
                boolean delOk = recordSet2.executeBatchSql(stringBuilder.toString(),params);
                if(!delOk){
                    baseBean.writeLog("paramsEroor===>中心岗位异动表数据插入失败!");
                }else{

                    baseBean.writeLog("paramsCorrect===>中心岗位异动表数据插入成功!");
                }
            }
            if(!paramsTwo.isEmpty()){
                boolean delOk = recordSet2.executeBatchSql(stringBuilderTwo.toString(),paramsTwo);
                if(!delOk){
                    baseBean.writeLog("paramsTwoEroor===>酒店岗位异动表数据插入失败!");
                }else{
                    baseBean.writeLog("paramsCorrect===>酒店岗位异动表数据插入成功!");
                }
            }
            //酒店代码+岗位名称 插入到ODS_Support_Centre_Scope，和DWD_Master_Hotel_Role中

        }
    }
}
